import {defineStore} from 'pinia'
import store from 'store2'
import _ from 'lodash'

const useWebSiteStore = defineStore('WebSiteStore',{
    state(){
        return{
            websites:[]
        }
    },

    actions:{
        add(website){
            const url = website.url
            if(this.websites.find(item=>item.url === url)){
                myApi.alert('此【'+url+'】网站已经被添加')
                return
            }
           this.websites.unshift(website)
           store.set('websites',this.websites)
        },
        init(){
            console.log('初始化')
            this.websites = store.get('websites')
            if(this.websites === null){
                this.websites = []
            } 
        },
        deleteItem(url){
            this.websites.splice(url,1)
            store.set('websites',this.websites)
        },
    },
    getters:{
         find(){
            return (keywords)=>{
                if(keywords === ''){
                    return this.websites
                }
                // 过滤this.websites
                const filterWebsits = this.websites.filter((item)=>{
                    let partten = new RegExp(keywords,'i')
                    return partten.test(item.title)
                })
                return filterWebsits
            }
         }   
    }
})

export default useWebSiteStore